home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / cenvid.zip / SOUND.BAT < prev    next >
DOS Batch File  |  1993-06-24  |  2KB  |  71 lines

  1. @echo off
  2. REM *******************************************************************
  3. REM *** Sound - Sound a specified frequency for specified number of ***
  4. REM ***         milliseconds (approximate).                         ***
  5. REM *******************************************************************
  6. CEnvi %0.bat %1 %2 %3
  7. GOTO CENVI_EXIT
  8.  
  9. main(argc,argv)
  10. {
  11.    if ( argc != 3 || 0 == (frequency=atol(argv[1])) || 0 == (duration=atol(argv[2])) )
  12.       Instructions();
  13.    else
  14.       sound(frequency,duration)
  15. }
  16.  
  17. sound(frequency,duration)  // frequency is in Hz, duration is in milliseconds
  18. {
  19.    StartTone(frequency);
  20.    Delay(duration);
  21.    StopTone();
  22. }
  23.  
  24. StartTone(frequency) // start 8253 programmable timer playing this frequency
  25. {
  26.    // determine counter to send to the 8253 programmable timer
  27.    #define  CHIP_RATE   1193180
  28.    counter = CHIP_RATE / frequency;
  29.    // program 8253
  30.    #define  COUNTER_REGISTER  0x42
  31.    #define  COMMAND_REGISTER  0x43
  32.    #define  SPEAKER_REGISTER  0x61
  33.    speaker = inport(SPEAKER_REGISTER);
  34.    if ( !(speaker & 0x3) ) {
  35.       speaker |= 0x3;
  36.       outport(SPEAKER_REGISTER,speaker);
  37.       outport(COMMAND_REGISTER,0xB6);
  38.    }
  39.    outport(COUNTER_REGISTER,counter & 0xFF);
  40.    outport(COUNTER_REGISTER,(counter >> 8) & 0xFF);
  41. }
  42.  
  43. StopTone()
  44. {
  45.    outport(SPEAKER_REGISTER,inport(SPEAKER_REGISTER) & 0xfc);
  46. }
  47.  
  48. Delay(duration) // duration is in milliseconds, approximately
  49. {
  50.    EndTime = clock() + (duration / 1000.0) * CLOCKS_PER_SEC;
  51.    while( clock() < endTime ) ;
  52. }
  53.  
  54.  
  55. Instructions()
  56. {
  57.    printf("\a\n")
  58.    printf("Sound - Sound a specified tone on the internal speaker for specified time\n")
  59.    printf("\n")
  60.    printf("SYNTAX:  SOUND Freqency Duration\n")
  61.    printf("\n")
  62.    printf("Where:  Frequency     Tone in hertz\n")
  63.    printf("        Duration      In milliseconds, accurate to %d milliseconds\n",1000/CLOCKS_PER_SEC)
  64.    printf("\n")
  65.    printf("The following example would play middle A for 2 seconds:\n");
  66.    printf("    SOUND 440 2000\n")
  67.    printf("\n")
  68. }
  69.  
  70. :CENVI_EXIT
  71.